package com.starblink.home.ui.adapter

import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.urllink.PullUpPageUtils
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.util.log.YYLogUtils
import com.youth.banner.adapter.BannerAdapter
import com.starblink.home.R
import com.starblink.rocketreserver.fragment.MaterialVOF
import java.util.ArrayList

/**
 * @author dyhpc
 */
class FeedsBannerAdapter(
    var modelPosition:Int,
    mDatas: ArrayList<MaterialVOF.MaterialDetailList?>,
    var context: Activity
) :
    BannerAdapter<MaterialVOF.MaterialDetailList, FeedsBannerAdapter.BannerViewHolder>(mDatas) {
    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view =
            LayoutInflater.from(context).inflate(R.layout.item_home_feeds_img_banner, parent, false)
        return BannerViewHolder(view)
    }

    override fun onBindView(
        holder: BannerViewHolder,
        data: MaterialVOF.MaterialDetailList?,
        position: Int,
        size: Int
    ) {
        data?.let {
            ImageUtils.loadImage(
                context,
                data.materialDetailVOF.linkUrl,
                holder.img,
                com.starblink.basic.style.R.drawable.one_page_skeleton_7,
                ImageUtils.errorImage
            )
            holder.img.setOnClickListener { v: View? ->

                YYLogUtils.e("资源位 ---》${data.materialDetailVOF.jumpUrl}")
                data.materialDetailVOF.jumpUrl?.let {
                    PullUpPageUtils.routePageIntercept4Url(
                        activity = context,
                        url = it
                    )
                }
            }
            holder.img.trackData(
                page = SpmPageDef.Home080Page_10164,
                element = SpmElementDef.HomeNewOperationSelect_33012,
                params = mapOf("id" to data.materialDetailVOF .materialId.toString() ,"index" to modelPosition.toString())
            )
        }
    }

    inner class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var img: AppCompatImageView

        init {
            img = view.findViewById(R.id.b_img)
        }
    }
}